package com.mingqijia.gassafety.shared.utils;

import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.enums.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.*;

/**
 * 对象映射工具类
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月12日 14:19
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MappingTools {

    public static final Timestamp DEFAULT_MIN_LDT =
            Timestamp.valueOf(LocalDateTime.of(1970, 1, 1, 0, 0, 0));

    public static final Timestamp DEFAULT_MAX_LDT =
            Timestamp.valueOf(LocalDateTime.of(2100, 12, 31, 23, 59, 59));

    public static long getCurrentSecond() {
        return toEpochSecond(LocalDateTime.now());
    }

    public static Timestamp getCurrent() {
        return Timestamp.valueOf(LocalDateTime.now());
    }

    public static long toCurrentSecond(Date date) {
        return date.getTime() / Constants.LONG_1_KILO;
    }

    public static long getCurrentAddMinutes(long minutes) {
        return toEpochSecond(LocalDateTime.now().plusMinutes(minutes));
    }

    public static long toEpochSecond(LocalDateTime ldt) {
        if (Objects.isNull(ldt)) {
            throw new NullPointerException();
        }
        return ldt.toEpochSecond(OffsetDateTime.now().getOffset());
    }

    public static RoleType toRoleType(int code) {
        return RoleType.parse(code);
    }

    public static Integer toRoleType(RoleType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static ManagerType toManagerType(int code) {
        return ManagerType.parse(code);
    }

    public static Integer toManagerType(ManagerType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static ParamType toParamType(int code) {
        return ParamType.parse(code);
    }

    public static Integer toParamType(ParamType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static PrivilegeType toPrivType(int code) {
        return PrivilegeType.parse(code);
    }

    public static Integer toPrivType(PrivilegeType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static ResourceType toResourceType(int code) {
        return ResourceType.parse(code);
    }

    public static Integer toResourceType(ResourceType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static RunningState toRunningState(int code) {
        return RunningState.parse(code);
    }

    public static Integer toRunningState(RunningState state) {
        if (Objects.isNull(state)) {
            return null;
        }
        return state.getCode();
    }

    public static SubjectType toSubjectType(int code) {
        return SubjectType.parse(code);
    }

    public static Integer toSubjectType(SubjectType type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static YesNoEnum toYesNoEnum(Integer code) {
        return YesNoEnum.parse(code);
    }

    public static YesNoEnum toYesNoEnum(String value) {
        return YesNoEnum.parse(value);
    }

    public static YesNoEnum toYesNoEnum(boolean flag) {
        return YesNoEnum.parse(flag);
    }

    public static Integer toYesNoEnum(YesNoEnum type) {
        if (Objects.isNull(type)) {
            return null;
        }
        return type.getCode();
    }

    public static List<String> fromRaw(String raw) {
        if (StringUtils.hasText(raw)) {
            return new ArrayList<>(Arrays.asList(raw.split(Constants.STR_COMMA)));
        }
        return Collections.emptyList();
    }

    public static String toRaw(List<String> raw) {
        if (CollectionUtils.isEmpty(raw)) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        raw.forEach(item -> builder.append(Constants.STR_COMMA).append(item));
        return builder.substring(Constants.INT_ONE);
    }
}
